import 'package:honeypotflutter/net/public_http.dart';
import 'package:honeypotflutter/publicBussine/view/bussine_modal.dart';
import 'package:honeypotflutter/public/public_total.dart';
import '../model/login_param.dart';

class LoginApi {
  /*
  * 密码登录
  * */
  static Future loginWithPassword(String phone, String pwd) async {
    Map<String, dynamic> param = {"usernameOrPhoneNumber": phone, "password": pwd, "rememberClient": "true", "tenancyName": "Default"};
    try {
      var result = await MGHttp.post("api/TokenAuth/AuthenticateByUsernameOrPhoneNumber", params: param, progress: "登录中...");
      return result;
    } catch (error) {
      MGModal.totalError(error);
      return Future.error(error);
    }
  }

  /*
  * 短信登录
  * */
  static Future loginWithCode(String phone, String code, String codeToken) async {
    Map<String, dynamic> param = {"phoneNumber": phone, "code": code, "codeToken": codeToken};
    try {
      var result = await MGHttp.post("api/TokenAuth/AuthenticateBySmsCaptcha", params: param, progress: "登录中...");
      return result;
    } catch (error) {
      MGModal.totalError(error);
      return Future.error(error);
    }
  }

  /*
  * 获取验证码
  * */
  static Future<String> sendMessage(String phone, int type) async {
    Map<String, dynamic> param = {"phoneNumber": phone, "type": type};
    try {
      var result = await MGHttp.post("api/services/app/SmsSendCode/SendVerificationCodeAsync", params: param);
      HLTotal.total("短信发送成功");
      return result;
    } catch (error) {
      MGModal.totalError(error);
      return Future.error(error);
    }
  }

  /*
  * 忘记密码
  * */
  static Future forget(LoginParam param) async {
    Map<String, dynamic> newParam = {
      "phoneNumber": param.phoneNumber,
      "code": param.code,
      "codeToken": param.codeToken,
      "password": param.password,
      "newPassword": param.password,
      "confirmPassword": param.password,
    };
    try {
      var result = await MGHttp.post("api/services/app/Account/RetrievePasswordByPhoneNumber", params: newParam, progress: "修改中...");
      return result;
    } catch (error) {
      MGModal.totalError(error);
      return Future.error(error);
    }
  }

  /*
  * 注册
  * */
  static Future register(LoginParam param) async {
    Map<String, dynamic> newParam = {"phoneNumber": param.phoneNumber, "code": param.code, "codeToken": param.codeToken, "password": param.password};
    try {
      var result = await MGHttp.post("api/services/app/Account/AppUserRegister", params: newParam, progress: "注册中...");
      return result;
    } catch (error) {
      MGModal.totalError(error);
      return Future.error(error);
    }
  }
}
